﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Model;
using RoadTeam.DAL;
using RoadTeam.Enums;

namespace RoadTeam.BLL
{
    public class SmExceptionBll
    {
        private String GetCreateTableStr()
        {
            String Str = @"if Not exists (select 1 from  sysobjects  where  id = object_id('SmException_" + DateTime.Now.Year + @"')  and   type = 'U')
create table SmException_" + DateTime.Now.Year + @" (
    ExceptionID          decimal       primary key       identity(1,1),
   CreateData           datetime             null,
   ExceptionMsg         varchar(500)         null,
   ExceptionStackTrace  text                 null,
   TargetSiteName       varchar(500)         null,
   TargetSiteFullName   varchar(500)         null,
   ExceptionSource      varchar(500)         null,
   RequestUrl           varchar(1000)        null,
   RequestPhysicalPath  varchar(2000)        null,
   BrowserInfo          varchar(2000)        null,
   UserHostName         varchar(500)         null,
   UserHostAddress      Varchar(500)         null,
   MethodName			Varchar(500)			NULL,
   Parameter					Text						NULL
)
";
            return Str;
        }

        public String GetExceptionInsertStr(SmException EX)
        {
            String Str = @"  INSERT INTO SmException_" + DateTime.Now.Year + @"(CreateData,ExceptionMsg,ExceptionStackTrace,
TargetSiteName,TargetSiteFullName,ExceptionSource,RequestUrl,
RequestPhysicalPath,BrowserInfo,UserHostName,UserHostAddress,MethodName,Parameter)
VALUES (getdate(),'" + EX.ExceptionMsg + "','" + EX.ExceptionStackTrace + "','" + EX.TargetSiteName +
 @"','" + EX.TargetSiteFullName + "','" + EX.ExceptionSource + "','" + EX.RequestUrl + "','" + EX.RequestPhysicalPath +
 @"','" + EX.BrowserInfo + "','" + EX.UserHostName + "','" + EX.UserHostAddress + "','" + EX.MethodName + "','" + EX .Parameter+ "')";
            return Str;
        }

        public void InSertException(SmException EX)
        {
            String SqlStr = GetCreateTableStr();

            DbFactory.CreateInstance().CreateDb().ExecuteStrQuery(SqlStr, System.Data.CommandType.Text, GlobalConfig.ConnectionString);
            SqlStr = GetExceptionInsertStr(EX);
            DbFactory.CreateInstance().CreateDb().ExecuteStrQuery(SqlStr, System.Data.CommandType.Text, GlobalConfig.ConnectionString);
   
        //这里添加对MOGODB的支持
        
        
        }
    }
}
